package com.mall.orders.service;

import cn.hutool.extra.spring.SpringUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mall.model.domain.OmsOrder;
import com.mall.model.domain.OmsOrderItem;
import com.mall.model.domain.OmsOrderOperateHistory;
import com.mall.model.vo.CommonPage;
import com.mall.model.vo.OmsOrderVO;
import com.mall.orders.mapper.OmsOrderMapper;
import com.mall.orders.service.OmsOrderService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 订单表(OmsOrder)表服务实现类
 *
 * @author wishmumu
 * @since 2023-03-11 15:42:52
 */
@Service
public class OmsOrderService {

    @Autowired
    private OmsOrderMapper orderMapper;

    public CommonPage selectAll(Integer pageNum, Integer pageSize, Integer sourceType, Integer status, String createTime, String orderSn, Integer orderType, String receiverKeyword) {
        Integer page = (pageNum - 1) * pageSize;
        List<OmsOrder> orderList = orderMapper.selectAll(page, pageSize, sourceType, status, createTime, orderSn, orderType, receiverKeyword);
        Integer count = orderMapper.count(sourceType, status, createTime, orderSn, orderType, receiverKeyword);
        return new CommonPage(pageNum, pageSize, count, orderList);
    }

    public void delete(List<Integer> ids) {
        orderMapper.delete(ids);
    }

    public void updateToClose(List<Integer> ids, String note) {
        Integer status = 4;
        orderMapper.updateToClose(ids, note, status);
    }

    public void updateDelivery(List<Map<String, Object>> deliveryParamList) {
        for (Map<String, Object> map : deliveryParamList) {
            String deliveryCompany = (String) map.get("deliveryCompany");
            String deliverySn = (String) map.get("deliverySn");
            Integer orderId = (Integer) map.get("orderId");
            orderMapper.updateDelivery(orderId, deliveryCompany, deliverySn);
        }
    }

    public void updateMoneyInfo(Map<String, Object> map) {
            Integer discountAmount = (Integer) map.get("discountAmount");
            Integer freightAmount = (Integer) map.get("freightAmount");
            Integer orderId = (Integer) map.get("orderId");
            Integer status = (Integer) map.get("status");
            orderMapper.updateMoneyInfo(orderId, discountAmount, freightAmount, status);

    }

    public void updateNote(Integer id, Integer status, String note) {
        orderMapper.updateNote(id, status, note);
    }

    public void updateReceiverInfo(Map<String, Object> map) {
            Integer orderId = (Integer) map.get("orderId");
            Integer status = (Integer) map.get("status");
            String receiverCity = (String) map.get("receiverCity");
            String receiverDetailAddress = (String) map.get("receiverDetailAddress");
            String receiverName = (String) map.get("receiverName");
            String receiverPhone = (String) map.get("receiverPhone");
            String receiverPostCode = (String) map.get("receiverPostCode");
            String receiverProvince = (String) map.get("receiverProvince");
            String receiverRegion = (String) map.get("receiverRegion");
            orderMapper.updateReceiverInfo(orderId, status, receiverCity, receiverDetailAddress, receiverName,
                                             receiverPhone, receiverPostCode, receiverProvince, receiverRegion);

    }

    public OmsOrderVO selectById(Integer id) {
        OmsOrder omsOrder = orderMapper.selectOrderById(id);
        List<OmsOrderItem> omsOrderItem = orderMapper.selectItemById(id);
        List<OmsOrderOperateHistory> omsOrderOperateHistory = orderMapper.selectHistoryById(id);
        OmsOrderVO omsOrderVO = new OmsOrderVO(omsOrderItem, omsOrderOperateHistory);
        BeanUtils.copyProperties(omsOrder,omsOrderVO);
        return omsOrderVO;
    }
}

